package cn.com.w.redis.use.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * redis 配置类
 */
@Configuration
public class RedisConfig {


    @Bean("redisTemplate")
    @Qualifier
    public RedisTemplate<String, ?> redisTemplate(RedisConnectionFactory connectionFactory){
        // 创建 RedisTemplate 对象
        RedisTemplate<String, ?> redisTemplate = new RedisTemplate<>();
        // 设置链接工厂
        redisTemplate.setConnectionFactory(connectionFactory);
        // 设置 字符串 key的序列化
        redisTemplate.setKeySerializer(RedisSerializer.string());
        // 设置 字符串 value的序列化
        redisTemplate.setValueSerializer(RedisSerializer.json());
        // 设置 hash key的序列化
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        // 设置 hash value的序列化
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
}
